using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE80;
using EnvDTE;
using Koda.VisualStudio;
using Koda.Utils;

namespace Koda.UI
{
    public partial class GenerateConstructorForm : Form
    {
        private CodeClass2 codeClass;
        private CodeVariable2[] codeVariables;

        public GenerateConstructorForm(CodeClass2 codeClass)
        {
            if (codeClass == null)
                throw new ArgumentNullException("codeClass2");
            
            this.codeClass = codeClass;
            this.codeVariables = VsCodeModelUtilities.GetVariables(this.codeClass);
            this.AutoScaleMode = AutoScaleMode.None;
            this.Font = SystemFonts.MessageBoxFont;
            this.AutoScaleMode = AutoScaleMode.Font;

            this.InitializeComponent();            
        }

        private void SelectAll()
        {
            for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
            {
                this.checkedListBox1.SetItemChecked(i, true);
            }        
        }

        private void SelectNone()
        {
            for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
            {
                this.checkedListBox1.SetItemChecked(i, false);
            }
        }

        private void ConstructorForm_Load(object sender, EventArgs e)
        {
            foreach (CodeVariable2 codeVariable in this.codeVariables)
            {
                this.checkedListBox1.Items.Add(codeVariable.Name);
            }

            // Check if the class already has a constructor
            if (VsCodeModelUtilities.ContainsMember(this.codeClass, this.codeClass.Name))
            {
                // Yes, select all fields
                this.SelectAll();
            }
        }

        private void acceptButton_Click(object sender, EventArgs e)
        {
            List<CodeVariable2> parameters = new List<CodeVariable2>();

            foreach (object codeVariableName in this.checkedListBox1.CheckedItems)
            {
                foreach (CodeVariable2 codeVariable in this.codeVariables)
                {
                    if (codeVariable.Name == codeVariableName.ToString())
                    {
                        parameters.Add(codeVariable);
                        break;
                    }
                }
            }

            DTE dte = this.codeClass.DTE;
            dte.UndoContext.Open("Generate Constructor", false);

            try
            {
                vsCMAccess accessModifier = vsCMAccess.vsCMAccessPublic;
                if (this.privateRadioButton.Checked)
                    accessModifier = vsCMAccess.vsCMAccessPrivate;

                if (VsCodeModelUtilities.IsConstructorDefined(this.codeClass, parameters.ToArray()))
                {
                    MessageBox.Show("Construcor is already defined!");
                }
                else
                {
                    VsCodeModelUtilities.GenerateConstructor(this.codeClass, parameters.ToArray(), this.generateCommentsCheckBox.Checked, accessModifier);
                }
            }
            catch (Exception ex)
            {
                dte.UndoContext.SetAborted();
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (dte.UndoContext.IsOpen)
                {
                    dte.UndoContext.Close();
                }
            }
        }

        private void selectAllButton_Click(object sender, EventArgs e)
        {
            this.SelectAll();
        }

        private void selectNoneButton_Click(object sender, EventArgs e)
        {
            this.SelectNone();
        }
    }
}